package org.management.system.controller;

import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.management.base.BaseController;
import org.management.base.data.PermissionCode;
import org.management.base.data.Result;
import org.management.system.entity.RoleEntity;
import org.management.system.entity.UserEntity;
import org.management.system.service.RoleService;
import org.management.system.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@Api(tags="用户模块")
@RequestMapping(value = "/users")
public class UserController extends BaseController<UserEntity> {
	
	@Resource
	private UserService userService;

	@Resource
    private RoleService roleService;

	@PostMapping(value="/{id}/role")
	@ApiOperation("用户设置默认角色")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "用户ID",dataType = "String",required = true),
        @ApiImplicitParam(name = "roleId",value = "角色ID",dataType = "String",required = true)
    })
	@RequiresPermissions(value = PermissionCode.USER_WRITE_EDIT)
	public Result setDefaultRole(
	        @PathVariable @NotEmpty(message = "用户ID不能为空") String id,
            @NotEmpty(message = "角色ID不能为空") String roleId) {
		this.userService.setDefaultRole(id,roleId);
		return Result.success("设置默认角色成功");
	}

	@PostMapping(value="/{id}/roles")
	@ApiOperation("保存用户信息及关联角色组")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "用户ID",dataType = "String",required = true),
        @ApiImplicitParam(name = "roleIds",value = "角色组",dataType = "String",required = true)
    })
	@RequiresPermissions(value = PermissionCode.USER_WRITE_EDIT)
	public Result saveEntity(@PathVariable
                             @NotEmpty(message = "用户ID不能为空") String id,
                             @NotEmpty(message = "角色组不能为空") String roleIds) {
        this.userService.saveUserWithRoles(id,roleIds);
		return Result.success("设置用户关联角色成功");
	}

	@PostMapping(value="/{id}/photo")
	@ApiOperation("保存用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户ID",dataType = "String",required = true),
            @ApiImplicitParam(name = "photo",value = "头像",dataType = "String",required = true)
    })
	@RequiresPermissions(value = PermissionCode.USER_WRITE_EDIT)
	public Result savePhoto(@PathVariable @NotEmpty(message = "用户ID不能为空") String id, String photo) {
	    UserEntity userEntity = new UserEntity();
	    userEntity.setId(id);
	    userEntity.setPicture(photo);
        this.userService.save(userEntity);
		return Result.success("保存头像成功");
	}

    @GetMapping(value="/{id}/role")
    @ApiOperation("获取用户默认角色")
    @ApiImplicitParam(name = "id",value = "用户ID",dataType = "String",required = true)
    @RequiresPermissions(value = PermissionCode.USER_READ_LIST)
    public RoleEntity getDefaultRoleId(@PathVariable @NotEmpty(message = "用户ID不能为空") String id) {
        return this.userService.getDefaultRole(id);
    }

    /**
     * 根据用户id获取角色集合 如为空 获取当前用户id
     * @param id 用户id
     * @return 角色集
     */
    @GetMapping(value="/{id}/roles")
    @ApiOperation("根据用户id获取角色集合")
    @ApiImplicitParam(name = "id",value = "用户ID",dataType = "String",required = true)
    @RequiresPermissions(value = PermissionCode.USER_READ_LIST)
    public List<RoleEntity> getRolesByUserId(@PathVariable @NotEmpty(message = "用户ID不能为空") String id){
        return this.roleService.getRoleByUserId(id);
    }

    /**
     * 根据id 获取角色id字符串
     * @param id 角色id
     * @return String
     */
    @GetMapping(value="/{id}/roleIds")
    @ApiOperation("根据id 获取角色ids字符串")
    @ApiImplicitParam(name = "id",value = "用户ID",dataType = "String")
    @RequiresPermissions(value = PermissionCode.USER_READ_LIST)
    public String getRoleIdsByUserId(@PathVariable @NotEmpty(message = "用户ID不能为空") String id) {
        return this.roleService.getRoleIdsByUserId(id);
    }
}
